// pages/heaith/health.js
const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        barHeight: App.globalData.paddingTop,

        uid: '',
        page: 1,
        page_size: 8,
        // 文章总数
        count: '',

        article_list: [],
        current_index: 0,
        swiper_list: [],
        styledata: {
            "bg_color": "white",
            "color": "#000",
            "name": "健康自测"
        },

        noMore: false,

        tabbarshow: true,

        controlStatus: false
    },

    isiOS: false,
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

        this.init_data(options)
        this.getSwiper()
        this.get_article_list2()

        // const that = this
        // wx.getSystemInfo({
        //     success(res) {
        //         console.log('机型信息', res.system);
        //         let iosReg = /iOS/
        //         if (iosReg.test(res.system)) {
        //             that.isiOS = true
        //         }

        //         console.log('this.isiOS', that.isiOS);
        //     }
        // })
    },
    // 初始化数据
    init_data(options) {
        this.setData({
            controlStatus: App.globalData.controlStatus
        })
    },

    // 处理分页器得点击跳转
    handlejump(e) {
        let page = e.detail.currentTarget.dataset.page
        this.setData({
            page
        })
        this.get_article_list2()
        // console.log('处理分页器得点击跳转', e);
        // console.log(page);
    },

    // 使用分页器方式的文章请求方法
    async get_article_list2() {
        let params = {
            page: this.data.page,
            limit: this.data.page_size
        }
        let res = await request.postall(api.articlelist, params)
        // console.log(res);
        res.data.forEach(item => {
            let readnum = parseInt(Math.random() * 400) + 700
            let sharenum = parseInt(Math.random() * 100) + 100
            item.readnum = readnum
            item.sharenum = sharenum
        });

        this.setData({
            article_list: res.data,
            count: res.count
        })
    },

    getSwiper() {
        let params = {
            type: 'articles',
            // page: 2,
            // limit: 5
        };

        let videoEntry = {
            enable: true,
            id: 9,
            img: "http://img.qian.gzzsu.com/shop/20240308/cznzri3nf8foswnhl6.jpg",
            media_type: "img",
            name: "test2",
            poster: "",
            url: "70",
            video: "",
        }

        request.post(api.getSwiper, params).then(res => {
            let filterRes = res.filter((item) => {
                if (item.media_type == 'video') return true
            })
            filterRes = filterRes.splice(0, 3)
            // filterRes.push(videoEntry)
            console.log(filterRes);
            this.setData({
                swiper_list: filterRes
            })
            // console.log(this.data.swiper_list)
        })

    },

    // 动态设置视频高度
    setWidthHeight(e) {
        util.hideLoading()

        let that = this
        let query = wx.createSelectorQuery()
        let outterbox = query.select(".pointbox")
        outterbox.fields({
            rect: true,
            dataset: true,
            size: true,
            scrollOffset: true,
            properties: ['scrollX', 'scrollY'],
            computedStyle: ['margin', 'backgroundColor'],
            context: true,
        }, function (res) {
            let oo = e.detail.height / e.detail.width
            let newHeight = oo * res.width
            // console.log('获取outterbox', res);
            // console.log(e.detail);
            // console.log('newHeight', newHeight);
            that.setData(
                {
                    newHeight: `${newHeight}px;`
                }
            )
        }).exec()
    },

    // 跳转视频列表页
    jumpVideoList() {
        wx.navigateTo({
            url: '/pages/videoplay/videolist',
            success: (result) => {

            },
            fail: () => { },
            complete: () => { }
        });
    },

    // 切换轮播暂停视频播放
    indicator_change(event) {
        // console.log('66666', event);
        // 轮播总长度
        let length = this.data.swiper_list.length
        let newIndex = event.detail.current
        let lastIndex, nextIndex

        // 将正在展示的轮播index，左右视频暂停
        if (newIndex == 0) {
            lastIndex = length - 1
            nextIndex = 1
        } else if (newIndex == length - 1) {
            lastIndex = length - 2
            nextIndex = 0
        } else {
            lastIndex = newIndex - 1
            nextIndex = newIndex + 1
        }

        // this.videoContext = wx.createVideoContext(`myVideo${newIndex}`)
        this.LvideoContext = wx.createVideoContext(`myVideo${lastIndex}`)
        this.NvideoContext = wx.createVideoContext(`myVideo${nextIndex}`)

        this.LvideoContext.pause()
        this.NvideoContext.pause()

    },

    go_detail(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/pages/health/health_detail?id=' + id,
        })
    },
    go_swiper_detail(e) {
        let id = e.currentTarget.dataset.url
        wx.navigateTo({
            url: '/pages/health/health_detail?id=' + id,
        })
    },

    // 上拉加载方式的文章请求方法
    async get_article_list() {
        let params = {
            page: this.data.page,
            limit: this.data.page_size
        }
        let res = await request.post(api.articlelist, params)
        if (res.length == 0) {
            this.setData({
                noMore: true
            })
            util.showToast('无更多内容')
            return
        }
        res.forEach(item => {
            let readnum = parseInt(Math.random() * 400) + 700
            let sharenum = parseInt(Math.random() * 100) + 100
            item.readnum = readnum
            item.sharenum = sharenum
        });
        let list = this.data.article_list
        let new_list = list.concat(res)
        this.setData({
            article_list: new_list
        })
    },

    screenChange(e) {
        // 如果非ios，直接退出
        console.log('!this.isiOS', this.isiOS);
        if (!this.isiOS) return

        console.log(e.detail);
        if (e.detail.fullScreen) {
            this.setData({
                tabbarshow: false
            })
        } else {
            this.setData({
                tabbarshow: true
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        // let swiperlist = this.data.swiper_list
        // swiperlist.forEach((item,index) => {
        //     if (item.mid == "video") {
        //         // 获取视频组件对象
        //         this.videoContext = wx.createVideoContext('myVideo')
        //     }
        // });
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        // 自定义底部栏保证选择无误的代码
        if (typeof this.getTabBar === 'function' &&
            this.getTabBar()) {
            this.getTabBar().setData({
                cartnum: App.globalData.cartnum,
                selected: 2
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    async onPullDownRefresh() {
        setTimeout(async () => {
            this.setData({
                page: 1,
                article_list: [],
                noMore: false
            })
            this.getSwiper()
            await this.get_article_list2()
            wx.stopPullDownRefresh()
        }, 1800);
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    // onReachBottom() {
    //     if (!this.data.noMore) {
    //         this.setData({
    //             page: ++this.data.page,
    //         })
    //         this.get_article_list();
    //     }
    // },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})